<?php
	
	// get library
	require_once(dirname(dirname(__FILE__)).'/includes/library.php');
	$helper = new HELPER();
	$helper->gatekeeper();
        
	$config = new CONFIG();
        
	// parse language constants
	require_once(dirname(dirname(__FILE__)).'/components/com_estateagent/languages/russian.php');

	// get type options from command line or session
	if (isset($_SESSION['context'])) $context = $_SESSION['context'];
	else $context = 'all';
	switch ($context) {
		case 'rent-long':
			$type = ' e.type = 1 AND ';
		break;
		case 'rent-day':
			$type = ' e.type = 2 AND ';
		break;
		case 'sale':
			$type = ' e.type = 3 AND ';
		break;
		default:
			$type = '';
		break;
	}
	
	// count objects
	if (isset($_POST['limit'])) $limit = $_POST['limit'];
	else $limit = 25;

	// object start
	if (isset($_POST['offset'])) $offset = $_POST['offset'];
	else $offset = 0;
	
	// fields reference
	$fields = " e.id, e.title, e.type, e.hot, e.newmn, e.mnfio, e.is_agent, c.cb_agent, u.name AS owner_name, "
			 ." e.publish_up, e.published, e.publish_down, e.created_by, "
			 ." e.price, e.rent, e.commis, e.secure, e.rent_extra, "
			 ." e.street, e.streetnr, e.microstate, e.locstate, e.town, e.district, "
			 ." e.fireside, e.toheat_ufloor, e.aircondition, e.tv, e.dsl, e.balcony, "
			 ." e.swpool, e.garage, e.terrace, e.lift";
	
	// publish options		 
	$today = date('Y-m-d');
	$publish_range = " (((e.publish_up >= '0000-00-00' AND e.publish_up <= '$today') AND e.publish_down >= '$today') "
       				. " OR ((e.publish_up >= '0000-00-00' AND e.publish_up <= '$today' ) AND (e.publish_down = '0000-00-00' OR e.publish_down >= '$today')) "
       				. " OR (e.publish_up = '0000-00-00' AND e.publish_down = '0000-00-00')) ";
	
    // addtional options   				
	$aditional_options = " AND $type e.published = 1 AND e.block = 0 AND e.older = 0 ";

	// connect to db & get data
	$dbo = new DB();
	$dbo->connect();
	$query = "SELECT $fields "
			."FROM rlt_estateagent AS e "
			."LEFT JOIN (rlt_comprofiler AS c, rlt_users AS u) ON (c.user_id = e.created_by AND u.id = e.created_by) "
			."WHERE "
			.$publish_range
			.$aditional_options
			."LIMIT $offset, $limit";

	$objects = $dbo->getObjectsArray($query);
	
	// get SEF links
	$query = "SELECT * FROM rlt_redirection";
	$seflinks = $dbo->getObjectsArray($query);
	$seflinks = $helper->sefLinksToArray($seflinks);
	
	// get category links
	$showpt_link = 'index.php?option=com_estateagent&act=cat&task=showPT&Itemid=14';
	if (array_key_exists($showpt_link, $seflinks)) $showpt_link = $seflinks[$showpt_link];
	
	$showae_link = 'index.php?option=com_estateagent&act=cat&task=showAE&Itemid=14';
	if (array_key_exists($showae_link, $seflinks)) $showae_link = $seflinks[$showae_link];
	
	$showpe_link = 'index.php?option=com_estateagent&act=cat&task=showPE&Itemid=14';
	if (array_key_exists($showpe_link, $seflinks)) $showpe_link = $seflinks[$showpe_link];
	
	$showre_link = 'index.php?option=com_estateagent&act=cat&task=showRE&Itemid=14';
	if (array_key_exists($showre_link, $seflinks)) $showre_link = $seflinks[$showre_link];
	
	// return objects html
	if (is_array($objects)) {
		foreach($objects as $obj) {
			$obj->link = 'index.php?option=com_estateagent&act=object&task=showEO&Itemid=14&id='.$obj->id;
			if (array_key_exists($obj->link, $seflinks)) $obj->link = $seflinks[$obj->link];
			
			$obj->type_link = 'index.php?option=com_estateagent&act=cat&task=showTP&id='.$obj->type.'&Itemid=14';
			if (array_key_exists($obj->type_link, $seflinks)) $obj->type_link = $seflinks[$obj->type_link];
			
			$obj->addbm_link = 'index.php?option=com_estateagent&Itemid=14&act=object&task=addBM&id='.$obj->id;
			if (array_key_exists($obj->addbm_link, $seflinks)) $obj->addbm_link = $seflinks[$obj->addbm_link];
			
			$obj->delbm_link = 'index.php?option=com_estateagent&Itemid=14&act=object&task=delBM&id='.$obj->id;
			if (array_key_exists($obj->delbm_link, $seflinks)) $obj->delbm_link = $seflinks[$obj->delbm_link];
			
			$obj->showpt_link = $showpt_link;
			$obj->showae_link = $showae_link;
			$obj->showpe_link = $showpe_link;
			$obj->showre_link = $showre_link;
			
			include(dirname(__FILE__).'/objshort.php'); 
		}
	}
?>